<?php
require_once ('Controller/Admin/Common.inc.php');
require_once ('View/Result.inc.php');
require_once ('Model/CircleModel.inc.php');
require_once ('Model/DeviceModel.inc.php');
require_once ('Model/DeviceToCircleModel.inc.php');
final class Controller extends common {
//	private $Loader;
	private $CircleModel;
	private $DeviceModel;
	private $DeviceToCircleModel;
	private $diaplayrows = 20;
	public function execude () {
		$this->CircleModel = new CircleModel ($this->mysql);
		$this->DeviceModel = new DeviceModel ($this->mysql);
		$this->DeviceToCircleModel = new DeviceToCircleModel ($this->mysql);
		$trace = debug_backtrace ();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
		if ($this->checklogin ()) {
			if (array_key_exists ('action', $_REQUEST)) {
				switch ($_REQUEST['action']) {
					case 'deviceinfo':
						$this->circlelistPage ();
						break;
					case 'addmapping':
						$this->addMappingPage ();
						break;
					case 'delcircle':
						$this->delcirclePage ();
						break;
#					case 'delete':
#						$this->deletePage ();
#						break;
					default:
						$this->listPage ();
						break;
				}
			} else {
				$this->listPage ();
			}
		}
	}

	private function delcirclePage () {
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && array_key_exists ('cdid', $_REQUEST) && ctype_digit ($_REQUEST['cdid'])) {
			if ($this->DeviceToCircleModel->DelDeviceToCircle ($_REQUEST['cdid'])) {
				$this->template->addWarningMesage ('Delete Successful');
			} else {
				$this->template->addWarningMesage ('Record not exists');
			}
		} else {
			$this->template->addWarningMesage ('wrong parameter');
		}
		$this->circlelistPage ();
	}

	private function circlelistPage () {
//		$this->CircleModel = new CircleModel($this->mysql);
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && ($DeviceInfo = $this->DeviceModel->CheckDeviceItemExistByID ($_REQUEST['id'])) != NULL) {
		//if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && $this->DeviceToCircleModel->GetCirclesByDeviceCount ($_REQUEST['id']) > 0) {
#			print_r ($DeviceInfo);
#			print_r ($this->DeviceToCircleModel->GetCirclesByDevice ($_REQUEST['id']));


			$deviceinfotemplate = new template ();
			$deviceinfotemplate->load ('html/DeviceManagerDeviceInfo.html');
			$deviceinfo = $deviceinfotemplate->dom->documentElement;
			$deviceinfo = $this->template->dom->importNode ($deviceinfo, true);

			$addMappingButton = $this->template->query ('div/input[@id="addMappingButton"]', $deviceinfo)->item (0);
			$addMappingButton->setAttribute ('onClick', sprintf ('javascript:document.location.href="?module=%s&action=addmapping&id=%d";', $this->Loader->module, $_REQUEST['id']));


			$deviceinfoitem = $this->template->query ('table/tbody[@id="deviceinfo"]', $deviceinfo)->item (0);

			$node = $this->template->query ('tr/td[@id="Device_ID"]', $deviceinfoitem)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceInfo[$this->DeviceModel->idRowName]);

			$node = $this->template->query ('tr/td[@id="IMEI"]', $deviceinfoitem)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceInfo[$this->DeviceModel->imeiRowName]);

			$node = $this->template->query ('tr/td[@id="Circles_Count"]', $deviceinfoitem)->item (0);
			$Count = $this->DeviceToCircleModel->GetCirclesByDeviceCount ($DeviceInfo[$this->DeviceModel->idRowName]);
			$node->nodeValue = htmlspecialchars ($Count);

			$node = $this->template->query ('table/tr/td/table/tbody[@id="circlelistitem"]', $deviceinfo)->item (0);
			$devicelist = $node->parentNode;
			if ($Count > 0) {
				$this->CircleModel = new CircleModel ($this->mysql);
				$devicelistitem = $node;
				$devicelist->removeChild ($node);
				foreach ($this->DeviceToCircleModel->GetCirclesByDevice ($_REQUEST['id']) as $DeviceToCircleItem) {
					$item = $devicelistitem->cloneNode (true);

					$CircleItem = $this->CircleModel->CheckCircleItemExist ($DeviceToCircleItem[$this->DeviceToCircleModel->circleIDRowName]);

					$node = $this->template->query ('tr/td[@id="circle_name"]', $item)->item (0);
					$node->nodeValue = htmlspecialchars ($CircleItem[$this->CircleModel->nameRowName]);

					$node = $this->template->query ('tr/td[@id="Devices_Count"]', $item)->item (0);
					$node->nodeValue = htmlspecialchars ($this->DeviceToCircleModel->GetDevicesByCircleCount ($CircleItem[$this->CircleModel->idRowName]));

					$node = $this->template->query ('tr/td[@id="DeviceToCircle_StartDateTime"]', $item)->item (0);
					$node->nodeValue = htmlspecialchars ($DeviceToCircleItem[$this->DeviceToCircleModel->startDateTimeRowName]);

					$node = $this->template->query ('tr/td[@id="DeviceToCircle_EndDateTime"]', $item)->item (0);
					$node->nodeValue = htmlspecialchars ($DeviceToCircleItem[$this->DeviceToCircleModel->endDateTimeRowName]);

					$node = $this->template->query ('tr/td[@id="Circle_Info"]/a', $item)->item (0);
					$node->setAttribute ('href', sprintf ('?module=%s&action=circleinfo&id=%d', 'CircleManager', $CircleItem[$this->CircleModel->idRowName]));

					$node = $this->template->query ('tr/td[@id="DeviceToCircle_Delete"]/a', $item)->item (0);
					$node->setAttribute ('href', sprintf ('?module=%s&action=delcircle&id=%d&cdid=%d', $this->Loader->module, $_REQUEST['id'], $DeviceToCircleItem[$this->DeviceToCircleModel->idRowName]));

					$devicelist->appendChild ($item);

				}

			} else {
				$devicelist->parentNode->parentNode->parentNode->removeChild ($devicelist->parentNode->parentNode);
			}


			$this->template->content->appendChild ($deviceinfo);

		} else {
			$this->template->addWarningMesage ('no record here');
			$this->listPage ();
		}
	}

#	private function deletePage () {
#		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id'])) {
#			$id = $_REQUEST['id'];
#			if ($this->DeviceModel->DelDeviceItem ($id)) {
#				$this->template->addWarningMesage ('Delete Successful');
#			} else {
#				$this->template->addWarningMesage ('Device not exists');
#			}
#			$this->listPage ();
#		} else {
#			$this->template->addWarningMesage ('wrong parameter');
#		}
#	}
////////////////////////////
	private function NaddMappingPage ($DeviceInfo, $id, $Circle_ID = '', $StartDateTime = '', $EndDateTime = '') {
		$deviceaddmappingtemplate = new template ();
		$deviceaddmappingtemplate->load ('html/DeviceManagerAddMapping.html');
		$deviceaddmapping = $deviceaddmappingtemplate->dom->documentElement;
		$deviceaddmapping = $this->template->dom->importNode ($deviceaddmapping, true);
		$deviceaddmapping->setAttribute ('action', sprintf ('?module=%s&action=addmapping&id=%d', $this->Loader->module, $id));
		$deviceinfoitem = $this->template->query ('table/tbody[@id="deviceinfo"]', $deviceaddmapping)->item (0);

		$node = $this->template->query ('tr/td[@id="Device_ID"]', $deviceinfoitem)->item (0);
		$node->nodeValue = htmlspecialchars ($DeviceInfo[$this->DeviceModel->idRowName]);

		$node = $this->template->query ('tr/td[@id="IMEI"]', $deviceinfoitem)->item (0);
		$node->nodeValue = htmlspecialchars ($DeviceInfo[$this->DeviceModel->imeiRowName]);

		$node = $this->template->query ('tr/td[@id="Circles_Count"]', $deviceinfoitem)->item (0);
		$Count = $this->DeviceToCircleModel->GetCirclesByDeviceCount ($DeviceInfo[$this->DeviceModel->idRowName]);
		$node->nodeValue = htmlspecialchars ($Count);
/////////////////////
		$node = $this->template->query ('table/tr/td/table/tr/td/select[@name="Circle_ID"]/option', $deviceaddmapping)->item (0);
		foreach ($this->CircleModel->GetCircleItems (0, $this->CircleModel->GetCircleCount ()) as $item) {
			$newnode = $node->cloneNode (true);
			if ($Circle_ID != '' && $item[$this->CircleModel->idRowName] == $Circle_ID) {
				$newnode->setAttribute ('selected', 'selected');
			}
			$newnode->setAttribute ('value', $item[$this->CircleModel->idRowName]);
			$newnode->nodeValue = htmlspecialchars ($item[$this->CircleModel->nameRowName]);
			$node->parentNode->appendChild ($newnode);
		}
		if ($StartDateTime != '') {
			$node = $this->template->query ('table/tr/td/table/tr/td/input[@name="StartDateTime"]', $deviceaddmapping)->item (0);
			$node->setAttribute ('value', $StartDateTime);
		}
		if ($EndDateTime != '') {
			$node = $this->template->query ('table/tr/td/table/tr/td/input[@name="EndDateTime"]', $deviceaddmapping)->item (0);
			$node->setAttribute ('value', $EndDateTime);
		}

		$this->template->content->appendChild ($deviceaddmapping);
	}

	private function addMappingPage () {
		if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && ($DeviceInfo = $this->DeviceModel->CheckDeviceItemExistByID ($_REQUEST['id'])) != NULL) {
			if ($_SERVER['REQUEST_METHOD'] == 'POST') {
				if (ctype_digit ($_REQUEST['Circle_ID']) && ($CircleInfo = $this->CircleModel->CheckCircleItemExist ($_REQUEST['Circle_ID'])) != NULL) {
					$DeviceToCircleInfo = $this->DeviceToCircleModel->GetCirclesByDevice ($_REQUEST['id']);
					if (count ($DeviceToCircleInfo) > 0) {
						if ($DeviceToCircleInfo[0][$this->DeviceToCircleModel->circleIDRowName] == $_REQUEST['Circle_ID']) {
							$this->template->addWarningMesage ('Edit Successful');
							$this->DeviceToCircleModel->UpdateDeviceToCircle ($DeviceToCircleInfo[0][$this->DeviceToCircleModel->idRowName], $_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
							$this->listPage ();
						} else {
							$this->template->addWarningMesage ('Device was maped');
							$this->NaddMappingPage ($DeviceInfo, $_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
						}
					} else {
						$this->template->addWarningMesage ('Add Successful');
						$this->DeviceToCircleModel->AddDeviceToCircle ($_REQUEST['id'], $_REQUEST['Circle_ID'], $_REQUEST['StartDateTime'], $_REQUEST['EndDateTime']);
						$this->listPage ();
					}
				}
			} else {
				$this->NaddMappingPage ($DeviceInfo, $_REQUEST['id']);
			}
		} else {
			$this->template->addWarningMesage ('no record here');
			$this->listPage ();
		}
	}

////////////////////////////
	private function listPage () {
		$page = 0;
		$totalpage = 0;
		$totalrows = $this->DeviceModel->GetDeviceCount ();

		if (array_key_exists ('page', $_REQUEST) && ctype_digit ($_REQUEST['page'])) {
			$page = $_REQUEST['page'];
		}
		$totalpage = ceil ($totalrows / $this->diaplayrows);
		if ($page != 0 && $totalpage <= $page) {
			$page = 0;
			$this->template->addWarningMesage ('no record here');
		}
		if ($totalpage == 0) {
			$totalpage = 1;
		}
		$devicelisttemplate = new template ();
		$devicelisttemplate->load ('html/DeviceManagerlist.html');
		$devicelistform = $devicelisttemplate->dom->documentElement;
		$devicelistform = $this->template->dom->importNode ($devicelistform, true);


		$node = $this->template->query ('table/tbody[@id="devicelistitem"]', $devicelistform)->item (0);
		$devicelist = $node->parentNode;
		$devicelistitem = $node;
		$devicelist->removeChild ($node);

		foreach ($this->DeviceModel->GetDeviceItems ($page * $this->diaplayrows, $this->diaplayrows) as $DeviceItem) {

			$item = $devicelistitem->cloneNode (true);

/*			$node = $this->template->query ('tr/td[@id="description"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->imeiRowName]);

			$node = $this->template->query ('tr/td[@id="pubDate"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->manufacturerRowName]);
			$devicelist->appendChild ($item);*/



//print_r ($this->DeviceToCircleModel->GetCirclesByDevice ($DeviceItem[$this->DeviceModel->idRowName]));

			$node = $this->template->query ('tr/td[@id="Device_ID"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->idRowName]);

			$node = $this->template->query ('tr/td[@id="IMEI"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->imeiRowName]);

/*			$node = $this->template->query ('tr/td[@id="PhoneNumber"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->phoneNumberRowName]);

			$node = $this->template->query ('tr/td[@id="SimSerialNumber"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->simSerialNumberRowName]);

			$node = $this->template->query ('tr/td[@id="IMSI"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->imsiRowName]);

			$node = $this->template->query ('tr/td[@id="Manufacturer"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->manufacturerRowName]);

			$node = $this->template->query ('tr/td[@id="Model"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->modelRowName]);

			$node = $this->template->query ('tr/td[@id="OS"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->osRowName]);

			$node = $this->template->query ('tr/td[@id="Version"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->versionRowName]);

			$node = $this->template->query ('tr/td[@id="Remark"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($DeviceItem[$this->DeviceModel->remarkRowName]);*/

			$node = $this->template->query ('tr/td[@id="Circles_Count"]', $item)->item (0);
			$node->nodeValue = htmlspecialchars ($this->DeviceToCircleModel->GetCirclesByDeviceCount ($DeviceItem[$this->DeviceModel->idRowName]));

			$node = $this->template->query ('tr/td[@id="Device_Info"]/a', $item)->item (0);
			$node->setAttribute ('href', sprintf ('?module=%s&action=deviceinfo&id=%d', $this->Loader->module, $DeviceItem[$this->DeviceModel->idRowName]));

			$devicelist->appendChild ($item);
		}

		$node = $this->template->query ('span[@id="totlepage"]', $devicelistform)->item (0);
		$node->nodeValue = htmlspecialchars ($totalpage);

		$node = $this->template->query ('span[@id="page"]', $devicelistform)->item (0);
		$node->nodeValue = htmlspecialchars ($page + 1);

		$node = $this->template->query ('a[@id="previouspage"]', $devicelistform)->item (0);
		if ($page == 0) {
			$node->setAttribute ('href', '#');
		} else {
			$node->setAttribute ('href', sprintf ('?module=%s&page=%d', $this->Loader->module, $page - 1));
		}
		$node = $this->template->query ('a[@id="nextpage"]', $devicelistform)->item (0);
		if ($page == $totalpage - 1) {
			$node->setAttribute ('href', '#');
		} else {
			$node->setAttribute ('href', sprintf ('?module=%s&page=%d', $this->Loader->module, $page + 1));
		}

		$this->template->content->appendChild ($devicelistform);

	}
}
?>
